//
//  AMBaseController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOBaseController.h"
#import "HORequestFactory.h"
#import "HOConstants.h"
#import "HOUIMgr.h"
#import "Logger.h"
#import "HOUserInfoDataModule.h"
#import "HOAppDataModule.h"
#import "HOMessageDataModule.h"
#import "HOMyDataModule.h"
#import "HOUtils.h"
#import "HODefine.h"
#import "HOStaticDataMgr.h"

@interface HOBaseController()
@end

@implementation HOBaseController
- (instancetype)initWithModuleType:(HO_MODULE_TYPE)type
{
    if(self = [super init])
    {
        viewListeners = [[NSMutableArray alloc] init];
        moduleType = type;
        [self initModule];
    }
    return self;
}

- (void)initModule
{
    switch (moduleType) {
        case HO_MODULE_TYPE_USER:
            dataModule = [[HOUserInfoDataModule alloc] initWithModuleType:moduleType];
            break;
        case HO_MODULE_TYPE_APP:
            dataModule = [[HOAppDataModule alloc] initWithModuleType:moduleType];
            break;
        case HO_MODULE_TYPE_MESSAGE:
            dataModule = [[HOMessageDataModule alloc] initWithModuleType:moduleType];
            break;
        case HO_MODULE_TYPE_MY:
            dataModule = [[HOMyDataModule alloc] initWithModuleType:moduleType];
            break;
        default:
            dataModule = [[HOAppDataModule alloc] init];
            break;
    }
}

#pragma mark - -- for UI listener --

- (void)addListener:(NSObject<HOViewControllerDelegate> *)view
{
    if(![viewListeners containsObject:view])
    {
        [viewListeners addObject:view];
    }
}

- (void)removeListener:(NSObject<HOViewControllerDelegate> *)view
{
    [viewListeners removeObject:view];
}


- (HOBaseDataModule *)getDataModule
{
    return dataModule;
}
#pragma mark - -- HOControllerDelegate implement mothods --
- (void)didReceiveCommand:(NSString *)action data:(id)data
{
    LogInf(@"viewListeners:%@", viewListeners);
    for(int i = 0; i < [viewListeners count]; i ++)
    {
        NSObject<HOViewControllerDelegate> *view = [viewListeners objectAtIndex:i];
        [view didReceiveCommand:action data:data];
    }
}
- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	
	if([response isRequestDone])
    {
        [[self getDataModule] didReceiveResponse:response];
        [self handleResponseLogic:response];
		
		if([response isEqualAction:kHO_ACTION_GetFaultLocationList])
		{
			[[HOStaticDataMgr sharedInstance] didReceiveFaultLocationList:response.dataNode];
		}
    }
    
//    LogInf(@"didReceiveResponse viewListeners：%@", viewListeners);
    for(int i = 0; i < [viewListeners count]; i ++)
    {
        NSObject<HOViewControllerDelegate> *view = [viewListeners objectAtIndex:i];
//        LogInf(@"view：%@", view);
		if(view)
		{
			[view didReceiveResponse:response];
		}
    }
}

/*
 Controller handle response logic, like login logic.
 */
 
- (void)handleResponseLogic:(HOBaseResponseEntity *)response
{
    
}
- (void)clearCache
{
    [dataModule cleanup];
}
- (void)cleanup
{
    [dataModule cleanup];
}

- (void)logout
{
    for(int i = 0; i < [viewListeners count]; i ++)
    {
        NSObject<HOViewControllerDelegate> *view = [viewListeners objectAtIndex:i];
        [view didReceiveCommand:kHO_ACTION_Logout data:nil];
	}
	[self cleanup];
	
}
- (void)didLogin
{
    for(int i = 0; i < [viewListeners count]; i ++)
    {
        NSObject<HOViewControllerDelegate> *view = [viewListeners objectAtIndex:i];
        [view didReceiveCommand:kHO_ACTION_Login data:nil];
    }
}

- (void)sendCommand:(NSString *)action data:(id)data
{
	[[HOUIMgr sharedInstance] sendCommand:action data:data];
}

- (void)sendRequestWithParams:(NSDictionary *)dic
{
	[self sendRequestWithParams:dic
					withModules:nil];
}

- (void)sendRequestWithRequest:(HOBaseRequestEntity *)request
{
	[[HOUIMgr sharedInstance] sendRequest:request];
}

- (void)sendRequestWithParams:(NSDictionary *)dic withModules:(NSArray *)modules
{
    NSString *action = [dic objectForKey:kHO_FIELD_Action];
    if(action == nil && action.length < 1)
    {
        return;
    }
    HOBaseRequestEntity *request = [[HORequestFactory sharedInstance] getRequest:action];
    
    for(NSString *key in dic)
    {
        [request addParam:[dic objectForKey:key] forKey:key];
    }
	if(modules)
	{
		for(int i = 0; i < [modules count]; i ++)
		{
			NSNumber *moduleNumber = [modules objectAtIndex:i];
			[request addModule:[moduleNumber intValue]];
		}
	}
	
    [[HOUIMgr sharedInstance] sendRequest:request];
}



#pragma mark
- (void)dealloc
{
    [viewListeners release];
    if(dataModule)
    {
        [dataModule release];
        dataModule = nil;
    }
    [super dealloc];
}
@end
